package com.yuke.cloud.service.pmc.service.impl;

import com.yuke.cloud.service.pmc.dao.ProdCategoryAttributeMapper;
import com.yuke.cloud.service.pmc.dto.ProdAttributeDto;
import com.yuke.cloud.service.pmc.dto.ProdCategoryAttributeDto;
import com.yuke.cloud.service.pmc.entity.ProdCategoryAttribute;
import com.yuke.cloud.service.pmc.entity.ProdSpecAttribute;
import com.yuke.cloud.service.pmc.service.ProdAttributeService;
import com.yuke.cloud.service.pmc.service.ProdCategoryAttributeService;
import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.pmc.service.ProdSpecAttributeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by zys on 2018/11/16.
 */
@Slf4j
@Service
@Transactional
public class ProdCategoryAttributeServiceImpl extends BaseService<ProdCategoryAttribute> implements ProdCategoryAttributeService {

    @Resource
    private ProdCategoryAttributeMapper prodCategoryAttributeMapper;
    @Resource
    private ProdAttributeService prodAttributeService;
    @Resource
    private ProdSpecAttributeService prodSpecAttributeService;

    @Override
    public int save(ProdCategoryAttribute record) {
        record.setCategoryAttributeId(generateId());
        return super.save(record);
    }

    @Override
    public void deleteByIds(String ids) {
        List<Long> cateAttrIds = Arrays.asList(ids.split(",")).stream().map(id -> Long.valueOf(id)).collect(Collectors.toList());
        List<ProdCategoryAttribute> prodCategoryAttributes = prodCategoryAttributeMapper.getAttributeIdsByCateAttrIds(cateAttrIds);
        List<Long> specAttributeIds = prodSpecAttributeService.getSpecAttributeIdsByCateAndAttr(prodCategoryAttributes);
        super.deleteByIds(ids);
        if (specAttributeIds.size()>0)
            prodSpecAttributeService.deleteBySpecAttributeIds(specAttributeIds);
    }

    @Override
    public List<ProdCategoryAttributeDto> getBindByCategoryId(Long categoryId) {
        return prodCategoryAttributeMapper.getBindByCategoryId(categoryId);
    }

    @Override
    public List<ProdCategoryAttributeDto> getEnableBindByCategoryId(Long categoryId) {
        return prodCategoryAttributeMapper.getEnableBindByCategoryId(categoryId);
    }

    @Override
    public List<ProdAttributeDto> getUnboundByCategoryId(Long categoryId) {
        List<ProdAttributeDto> prodAttributeDtos = prodAttributeService.getAttributeInfoByCategoryId(categoryId);
        return prodAttributeDtos;
    }

    @Override
    public List<ProdAttributeDto> getUnboundByCondition(Long categoryId, Long attributeCategoryId, String attributeName) {
        return prodCategoryAttributeMapper.getUnboundByCondition(categoryId, attributeCategoryId, attributeName);
    }
}
